﻿using System.Collections.Generic;
using TpBaseObjects.Base;
using TpBaseObjects.Objects;

namespace TpTools {
    public class GlobalVar {

        #region Members

        private static Dictionary<string, int> menuGroupsEntries = new Dictionary<string, int>();

        public struct MenuEntry {
            public string MenuName;
            public string Caption;
            public string OnAction;
            public int FaceID;
            public bool Enabled;
            public bool NewGroup;
            public bool Visibly;

            //Matrix for Enabled
            public bool Kalkulation;
            public bool Angebot;
            public bool Auftragsbestaetigung;
            public bool Rechnung;
            public bool ProjektBericht;
            public bool Abnahmeprotokoll;
            public bool EverySheet;
        }

        public enum VersandArt {
            Mail = 1,
            Fax,
            Brief
        }

        private static List<MenuEintrag> mList = new List<MenuEintrag>();
        private static List<MenuEintrag> mListIntern = new List<MenuEintrag>();
        public static Organisation currentOrganisation;
        public static Person currentPerson;
        private static List<MenuEntry> MenuList = new List<MenuEntry>();
        private static string summeGesamtFormel;
        private static int summenHierachie = 0;
        private static int lastSummenRow = 0;
        private static int summenStartRow = 0;
        private static int summenStartRow2 = 0;
        private static int summenStartRow3 = 0;
        private static int maxSummenHierachie = 0;
        private static int s4SummenStart = 0;
        private static bool copyRabatt = false;
        private static int previousSummenHirachie = 0;
        private static List<string> summenZellen = new List<string>();
        private static List<string> s1SummenZellen = new List<string>();
        private static List<string> s2SummenZellen = new List<string>();
        private static List<string> s3SummenZellen = new List<string>();
        private static List<string> produktZeile = new List<string>();
        private static int versand = (int)VersandArt.Mail;

        public const string tpContextMenuName = "Ply";
        
        #endregion

        #region Properties

        public static int Count {
            get { return (MenuList == null ? 0 : MenuList.Count); }
        }

        public static bool CopyRabatt {
            get { return copyRabatt; }
            set { copyRabatt = value; }
        }

        public static List<MenuEntry> Elements {
            get { return MenuList; }
        }

        public static int LastSummenRow {
            get { return lastSummenRow; }
            set { lastSummenRow = value; }
        }

        public static List<MenuEintrag> MenuEntries {
            get { return mList; }
        }

        public static List<MenuEintrag> MenuEntriesIntern {
            get { return mListIntern; }
        }

        public static int MaxSummenHierachie {
            get { return maxSummenHierachie; }
            set {
                if(value > 0) {
                    if(value > maxSummenHierachie) {
                        maxSummenHierachie = value;
                    }
                } else {
                    maxSummenHierachie = value;
                }
            }
        }

        public static int PreviousSummenHierachie {
            get { return previousSummenHirachie; }
            set { previousSummenHirachie = value; }
        }

        public static List<string> ProduktZeilen {
            get { return produktZeile; }
        }

        public static int ProduktZeilenAnzahl {
            get {
                return produktZeile.Count;
            }
        }

        public static string SummeGesamtFormel {
            get { return summeGesamtFormel; }
            set { summeGesamtFormel = value; }
        }

        public static int SummenHierachie {
            get { return summenHierachie; }
            set { summenHierachie = value; }
        }

        public static int SummenStartRow {
            get { return summenStartRow; }
            set { summenStartRow = value; }
        }

        public static int SummenStartRow2 {
            get { return summenStartRow2; }
            set { summenStartRow2 = value; }
        }

        public static int SummenStartRow3 {
            get { return summenStartRow3; }
            set { summenStartRow3 = value; }
        }

        public static List<string> SummenZellen {
            get { return summenZellen; }
        }

        public static List<string> S1SummenZellen {
            get { return s1SummenZellen; }
        }

        public static List<string> S2SummenZellen {
            get { return s2SummenZellen; }
        }

        public static List<string> S3SummenZellen {
            get { return s3SummenZellen; }
        }

        public static int S4SummenStart {
            get { return s4SummenStart; }
            set { s4SummenStart = value; }
        }

        public static int Versand {
            get { return versand; }
            set { versand = value; }
        }

        #endregion

        #region Functions

        public static bool EnableMenuGroup(int groupID) {
            if(menuGroupsEntries.ContainsKey("MenuGroup" + groupID.ToString())) {
                return true;
            }
            return false;
        }

        #endregion

        #region Methods

        public static void AddMenuGroups(int groupID) {
            if(!menuGroupsEntries.ContainsKey("MenuGroup" + groupID.ToString())) {
                menuGroupsEntries.Add("MenuGroup" + groupID.ToString(), groupID);
            }
        }

        public static void Add(MenuEntry value) {
            MenuList.Add(value);
        }

        public static void AddMenu(MenuEintrag menu) {
            mList.Add(menu);
        }

        public static void AddMenu(MenuEintrag menu, bool intern) {
            if(intern) {
                mListIntern.Add(menu);
            } else {
                mList.Add(menu);
            }
        }

        public static void AddProduktZeile(string zelle) {
            produktZeile.Add(zelle);
        }

        public static void AddSummenAddress(byte type, string zelle) {
            switch(type) {
                case 1:
                    s1SummenZellen.Add(zelle);
                    break;
                case 2:
                    s2SummenZellen.Add(zelle);
                    break;
                case 3:
                    s3SummenZellen.Add(zelle);
                    break;
                case 4:
                    summenZellen.Add(zelle);
                    break;
                default:
                    break;
            }
        }

        public static void RemoveAllMenuGroups() {
            for(int i = 1; i < 6; i++) {
                if(menuGroupsEntries.ContainsKey("MenuGroup" + i.ToString())) {
                    menuGroupsEntries.Remove("MenuGroup" + i.ToString());
                }
            }
        }

        public static void RemoveAllProduktZeilen() {
            string[] sCopy = new string[produktZeile.Count];
            produktZeile.CopyTo(sCopy);
            foreach(string s1 in sCopy) {
                produktZeile.Remove(s1);
            }
        }

        public static void RemoveAllSummenZeilen() {
            RemoveSummenAddress(1, true);
            RemoveSummenAddress(2, true);
            RemoveSummenAddress(3, true);
            RemoveSummenAddress(4, true);
        }

        public static void RemoveSummenAddress(byte type, bool del) {
            if(del) {
                switch(type) {
                    case 1:
                        string[] s1Copy = new string[s1SummenZellen.Count];
                        s1SummenZellen.CopyTo(s1Copy);
                        foreach(string s1 in s1Copy) {
                            s1SummenZellen.Remove(s1);
                        }
                        break;
                    case 2:
                        string[] s2Copy = new string[s2SummenZellen.Count];
                        s2SummenZellen.CopyTo(s2Copy);
                        foreach(string s1 in s2Copy) {
                            s2SummenZellen.Remove(s1);
                        }
                        break;
                    case 3:
                        string[] s3Copy = new string[s3SummenZellen.Count];
                        s3SummenZellen.CopyTo(s3Copy);
                        foreach(string s1 in s3Copy) {
                            s3SummenZellen.Remove(s1);
                        }
                        break;
                    case 4:
                        string[] s4Copy = new string[summenZellen.Count];
                        summenZellen.CopyTo(s4Copy);
                        foreach(string s1 in s4Copy) {
                            summenZellen.Remove(s1);
                        }
                        break;
                }
            }
        }

        public static void Delete(MenuEntry value) {
            MenuList.Remove(value);
        }

        #endregion

    }
}
